import util from '../../utils/util';
import api from '../../utils/api';
//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    code:''
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs',
      scene:""
    })
  },
  onLoad: function (options) {
    if (options.scene) {
      this.scene = options.scene;
      //将token存到本地存储中
      wx.setStorageSync('scene',options.scene);
    }
    let _this = this;
    let token = wx.getStorageSync('token');
    if(token != ''&&token != null){
      if (app.globalData.userInfo) {
        this.setData({
          userInfo: app.globalData.userInfo,
          hasUserInfo: true
        })
      } else if (this.data.canIUse){
        // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
        // 所以此处加入 callback 以防止这种情况
        app.userInfoReadyCallback = res => {
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      } else {
        // 在没有 open-type=getUserInfo 版本的兼容处理
        wx.getUserInfo({
          success: res => {
            app.globalData.userInfo = res.userInfo
            this.setData({
              userInfo: res.userInfo,
              hasUserInfo: true
            })
          }
        })
      }
    }else{
      wx.login({
        success(res) {
          console.log(res.code)
          wx.setStorageSync('code',res.code);
          _this.data.code = res.code;
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
   /**
   * 获取小程序二维码参数
   * @param {String} scene 需要转换的参数字符串
   */
  getScene: function(scene = "") {
    if (scene == "") return {}
    let res = {}
    let params = decodeURIComponent(scene).split("&")
    params.forEach(item => {
      let pram = item.split("=")
      res[pram[0]] = pram[1]
    })
    return res
  },
  goSign:function(e){
    wx.reLaunch({
      url: '/pages/select/select',
    })
  },
  bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
        //用户按了允许授权按钮
        var that = this;
            wx.getUserInfo({
              success: res => {
                console.log(res)
                //获取token
            api.post("evaluation/openid", {
              "code":that.data.code,
              "encryptedData":res.encryptedData,
              "vi":res.iv,
              "raw_data":res.rawData,
              "channel_id":that.scene
            }).then(res => {
                  console.log(res)
                  var token = res.data.token
                  //将token存到本地存储中
                  wx.setStorageSync('token',token);
                  //授权成功后，跳转进入小程序首页
                  wx.switchTab({
                    url: '/pages/select/select',
                  })
                }).catch(err => {
                    wx.showToast({
                        title: err.message,
                        icon: 'none'
                    })
                })   
          }
      })
    } else {
      wx.switchTab({
        url: '/pages/my/my',
      })
        //用户按了拒绝按钮
        // wx.showModal({
        //     title:'警告',
        //     content:'您点击了拒绝授权，将无法进入小程序，请授权之后再进入',
        //     showCancel:false,
        //     confirmText:'返回授权',
        //     success:function(res){
        //         if (res.confirm) {
        //             console.log('用户点击了“返回授权”')
        //         } 
        //     }
        // })
    }
},

  
})
